home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / CEGUISDK-0.4.1-VC6-STLport.exe / {app} / Samples / common / src / CEGuiD3D81BaseApplication.cpp next >
Encoding:
C/C++ Source or Header  |  2005-08-22  |  9.1 KB  |  284 lines

  1. /************************************************************************
  2.     filename:   CEGuiD3D81BaseApplication.cpp
  3.     created:    24/9/2004
  4.     author:     Paul D Turner
  5. *************************************************************************/
  6. /*************************************************************************
  7.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  8.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  9.  
  10.     This library is free software; you can redistribute it and/or
  11.     modify it under the terms of the GNU Lesser General Public
  12.     License as published by the Free Software Foundation; either
  13.     version 2.1 of the License, or (at your option) any later version.
  14.  
  15.     This library is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.     Lesser General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU Lesser General Public
  21.     License along with this library; if not, write to the Free Software
  22.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23. *************************************************************************/
  24. // this controls conditional compile of file for MSVC
  25. #include "CEGUIConfig.h"
  26. #ifdef CEGUI_SAMPLES_USE_DIRECTX_8
  27.  
  28. #include "CEGuiD3D81BaseApplication.h"
  29.  
  30. #include <d3d8.h>
  31. #include <dinput.h>
  32. // undefine Microsoft macro evilness
  33. #undef min
  34. #undef max
  35.  
  36. #include "renderers/directx81GUIRenderer/renderer.h"
  37. #include "CEGuiSample.h"
  38. #include "Win32AppHelper.h"
  39. #include "CEGUI.h"
  40.  
  41. #include <stdexcept>
  42.  
  43. #ifdef _MSC_VER
  44. # if defined(DEBUG) || defined (_DEBUG)
  45. #   pragma comment (lib, "DirectX81GUIRenderer_d.lib")
  46. # else
  47. #   pragma comment (lib, "DirectX81GUIRenderer.lib")
  48. # endif
  49. #endif
  50.  
  51. /*************************************************************************
  52.     Impl struct
  53. *************************************************************************/
  54. struct CEGuiD3D81BaseApplicationImpl
  55. {
  56.     HWND d_window;
  57.     LPDIRECT3D8 d_D3D;
  58.     LPDIRECT3DDEVICE8 d_3DDevice;
  59.     D3DPRESENT_PARAMETERS d_ppars;
  60.     CEGUI::DirectX81Renderer* d_renderer;
  61.     Win32AppHelper::DirectInputState d_directInput;
  62. };
  63.  
  64.  
  65. /*************************************************************************
  66.     Constructor
  67. *************************************************************************/
  68. CEGuiD3D81BaseApplication::CEGuiD3D81BaseApplication() :
  69.         pimpl(new CEGuiD3D81BaseApplicationImpl),
  70.         d_lastTime(GetTickCount()),
  71.         d_frames(0),
  72.         d_FPS(0)
  73. {
  74.     if (pimpl->d_window = Win32AppHelper::createApplicationWindow(800, 600))
  75.     {
  76.         if (initialiseDirect3D(800, 600, D3DADAPTER_DEFAULT, true))
  77.         {
  78.             if (Win32AppHelper::initialiseDirectInput(pimpl->d_window, pimpl->d_directInput))
  79.             {
  80.                 pimpl->d_renderer = new CEGUI::DirectX81Renderer(pimpl->d_3DDevice, 3000);
  81.  
  82.                 // initialise the gui system
  83.                 new CEGUI::System(pimpl->d_renderer);
  84.  
  85.                 CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative);
  86.  
  87.                 return;
  88.             }
  89.  
  90.             // cleanup direct 3d systems
  91.             pimpl->d_3DDevice->Release();
  92.             pimpl->d_D3D->Release();
  93.         }
  94.  
  95.         DestroyWindow(pimpl->d_window);
  96.     }
  97.     else
  98.     {
  99.         MessageBox(0, Win32AppHelper::CREATE_WINDOW_ERROR, Win32AppHelper::APPLICATION_NAME, MB_ICONERROR|MB_OK);
  100.     }
  101.  
  102.     throw std::runtime_error("Win32 DirectX 8.1 application failed to initialise.");
  103. }
  104.  
  105.  
  106. /*************************************************************************
  107.     Destructor.
  108. *************************************************************************/
  109. CEGuiD3D81BaseApplication::~CEGuiD3D81BaseApplication()
  110. {
  111.     Win32AppHelper::mouseLeaves();
  112.  
  113.     // cleanup gui system
  114.     delete CEGUI::System::getSingletonPtr();
  115.     delete pimpl->d_renderer;
  116.  
  117.     Win32AppHelper::cleanupDirectInput(pimpl->d_directInput);
  118.  
  119.     // cleanup direct 3d systems
  120.     pimpl->d_3DDevice->Release();
  121.     pimpl->d_D3D->Release();
  122.  
  123.     DestroyWindow(pimpl->d_window);
  124. }
  125.  
  126.  
  127. /*************************************************************************
  128.     Start the base application
  129. *************************************************************************/
  130. bool CEGuiD3D81BaseApplication::execute(CEGuiSample* sampleApp)
  131. {
  132.     sampleApp->initialiseSample();
  133.  
  134.     //
  135.     //  This is basically a modified Win32 message pump
  136.     //
  137.     bool idle;
  138.     HRESULT coop;
  139.  
  140.     while (Win32AppHelper::doWin32Events(idle))
  141.     {
  142.         if (idle)
  143.         {
  144.             // handle D3D lost device stuff
  145.             coop = pimpl->d_3DDevice->TestCooperativeLevel();
  146.  
  147.             if (coop == D3DERR_DEVICELOST)
  148.             {
  149.                 Sleep(500);
  150.                 continue;
  151.             }
  152.             else if (coop == D3DERR_DEVICENOTRESET)
  153.             {
  154.                 if (!resetDirect3D())
  155.                 {
  156.                     continue;
  157.                 }
  158.             }
  159.  
  160.             if (FAILED(pimpl->d_3DDevice->BeginScene()))
  161.             {
  162.                 continue;
  163.             }
  164.  
  165.             // main part of idle loop
  166.             updateFPS();
  167.             char fpsbuff[16];
  168.             sprintf(fpsbuff, "FPS: %d", d_FPS);
  169.  
  170.             Win32AppHelper::doDirectInputEvents(pimpl->d_directInput);
  171.  
  172.             // draw display
  173.             CEGUI::System& guiSystem = CEGUI::System::getSingleton();
  174.             pimpl->d_3DDevice->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  175.             guiSystem.renderGUI();
  176.  
  177.             // render FPS:
  178.             const CEGUI::Font* fnt = guiSystem.getDefaultFont();
  179.             if (fnt)
  180.             {
  181.                 guiSystem.getRenderer()->setQueueingEnabled(false);
  182.                 fnt->drawText(fpsbuff, CEGUI::Vector3(0, 0, 0), guiSystem.getRenderer()->getRect());
  183.             }
  184.  
  185.             pimpl->d_3DDevice->EndScene();
  186.  
  187.             pimpl->d_3DDevice->Present(0, 0, 0, 0);
  188.         }
  189.  
  190.         // check if the application is quitting, and break the loop next time
  191.         // around if so.
  192.         if (isQuitting())
  193.             PostQuitMessage(0);
  194.     }
  195.  
  196.     return true;
  197. }
  198.  
  199.  
  200. /*************************************************************************
  201.     Performs any required cleanup of the base application system.
  202. *************************************************************************/
  203. void CEGuiD3D81BaseApplication::cleanup()
  204. {
  205.     // nothing to do here.
  206. }
  207.  
  208.  
  209. /*************************************************************************
  210.     Initialise Direct3D system.
  211. *************************************************************************/
  212. bool CEGuiD3D81BaseApplication::initialiseDirect3D(unsigned int width, unsigned int height, unsigned int adapter, bool windowed)
  213. {
  214.     pimpl->d_D3D = Direct3DCreate8(D3D_SDK_VERSION);
  215.  
  216.     // display error and exit if D3D creation failed
  217.     if (pimpl->d_D3D)
  218.     {
  219.         D3DDISPLAYMODE d3ddm;
  220.         pimpl->d_D3D->GetAdapterDisplayMode(adapter, &d3ddm);
  221.  
  222.         D3DFORMAT format = d3ddm.Format;
  223.  
  224.         // complete window initialisation
  225.         ShowWindow(pimpl->d_window, SW_NORMAL);
  226.         UpdateWindow(pimpl->d_window);
  227.  
  228.         ZeroMemory(&pimpl->d_ppars, sizeof(pimpl->d_ppars));
  229.         pimpl->d_ppars.BackBufferFormat = format;
  230.         pimpl->d_ppars.hDeviceWindow    = pimpl->d_window;
  231.         pimpl->d_ppars.SwapEffect       = D3DSWAPEFFECT_DISCARD;
  232.         pimpl->d_ppars.Windowed         = windowed;
  233.  
  234.         if (!windowed)
  235.         {
  236.             pimpl->d_ppars.BackBufferWidth          = width;
  237.             pimpl->d_ppars.BackBufferHeight         = height;
  238.             pimpl->d_ppars.BackBufferCount          = 1;
  239.             pimpl->d_ppars.MultiSampleType          = D3DMULTISAMPLE_NONE;
  240.             pimpl->d_ppars.FullScreen_PresentationInterval  = D3DPRESENT_INTERVAL_IMMEDIATE;
  241.             pimpl->d_ppars.FullScreen_RefreshRateInHz       = D3DPRESENT_RATE_DEFAULT;
  242.         }
  243.  
  244.         if (SUCCEEDED(pimpl->d_D3D->CreateDevice(adapter, D3DDEVTYPE_HAL, pimpl->d_window, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pimpl->d_ppars, &pimpl->d_3DDevice)))
  245.         {
  246.             return true;
  247.         }
  248.         else
  249.         {
  250.             MessageBox(0, Win32AppHelper::CREATE_DEVICE_ERROR, Win32AppHelper::APPLICATION_NAME, MB_ICONERROR|MB_OK);
  251.         }
  252.  
  253.         pimpl->d_D3D->Release();
  254.         pimpl->d_D3D = 0;
  255.     }
  256.     else
  257.     {
  258.         MessageBox(0, Win32AppHelper::CREATE_D3D_ERROR, Win32AppHelper::APPLICATION_NAME, MB_ICONERROR|MB_OK);
  259.     }
  260.  
  261.     return false;
  262. }
  263.  
  264.  
  265. /*************************************************************************
  266.     Do reset of Direct3D device
  267. *************************************************************************/
  268. bool CEGuiD3D81BaseApplication::resetDirect3D(void)
  269. {
  270.     // perform ops needed prior to reset
  271.     pimpl->d_renderer->preD3DReset();
  272.  
  273.     if (SUCCEEDED(pimpl->d_3DDevice->Reset(&pimpl->d_ppars)))
  274.     {
  275.         // re-build stuff now reset has been done.
  276.         pimpl->d_renderer->postD3DReset();
  277.  
  278.         return true;
  279.     }
  280.  
  281.     return false;
  282. }
  283.  
  284. #endif